<template>
	<view class="comfirmOrder">
		<view class="address w95" @click="gotoChooseAddress" v-if="vuex_thisAddress">
			<view class="top">
				<view>
					<text>{{vuex_thisAddress.recive_name}}</text>
					<text>{{vuex_thisAddress.recive_phone}}</text>
				</view>
				<!-- <image class="edit" :src="vuex_httpImageUrl && vuex_httpImageUrl+'static/common/edit.png'" mode="widthFix"></image> -->
			</view>
			<view class="addr">{{vuex_thisAddress.recive_city}}{{vuex_thisAddress.recive_address}}</view>
		</view>
		<view v-else class="address w95" @click="gotoChooseAddress">选择地址</view>
		<view class="list w95">
			<!-- <view class="top">
				<text>订单编号:</text>
				<text>1231312123123131</text>
			</view> -->
			<view class="shopinfo">
				<image class="thumb" :src="obj.db.image" mode="widthFix"></image>
				<view>
					<view class="name">{{obj.db.name}}</view>
					<view class="specs">
						<text>{{obj.db.goodsjson[obj.active].format}} X {{obj.shopNum}}</text>
						<text>￥{{ obj.db.price}}</text>
					</view>
				</view>
			</view>
			<view class="msg">
				<text>备注:</text>
				<input type="text" v-model="message" placeholder="选填,请先和商家协商一致">
			</view>
		</view>
		<view class="isXy" @click="xy = !xy">
			<image v-if="xy" :src="vuex_httpImageUrl && vuex_httpImageUrl+'static/common/xy1.png'" mode="widthFix"></image>
			<image v-else :src="vuex_httpImageUrl && vuex_httpImageUrl+'static/common/xy.png'" mode="widthFix"></image>
			<text>我已阅读并同意</text>
			<text class="xy" @click.stop="xieyi">《商城购物协议》</text>
		</view>
		<view class="fooder">
			<!-- <text>共1件</text> -->
			<view class="total">
				<text>合计:</text>
				<text>￥{{parseFloat(price)*obj.shopNum}}</text>
			</view>
			<view class="submit" @click="buy">立即付款</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				obj:{},
				message:'',
				price:'',
				xy:false,
			};
		},
		onLoad(o) {
			this.obj = JSON.parse(decodeURIComponent(o.obj))
			this.price = this.obj.db.price
		},
		onShow() {
			console.log(this.vuex_thisAddress);
		},
		methods:{
			xieyi(){
				uni.navigateTo({
					url:'./xieyi'
				})
			},
			gotoChooseAddress(){
				uni.navigateTo({
					url:'/pages/my/address/address?status=back'
				})
			},
			buy(){
				this.$u.post('/appuser/myinfo').then(res=>{
					if(!res.data.name){
						uni.showModal({
							title:'提示',
							content:'购买功能需要先绑定个人信息',
							success:res=>{
								if(res.confirm){
									uni.navigateTo({
										url:'/pages/my/edit/edit'
									})
								}
							}
						})
						return 
					}
					uni.login({
						success:res=>{
							let code  = res.code
							if(!this.xy){this.$u.toast('请先阅读并同意购物协议');return}
							if(!this.vuex_thisAddress){this.$u.toast('请先选择收获地址');return}
							this.$u.post('/wxpay/order',{
								address_id:this.vuex_thisAddress.id,
								goods_id:this.obj.db.id,
								goods_format:this.obj.db.goodsjson[this.obj.active].format,
								goods_num:this.obj.shopNum,
								message:this.message,
								phone_status:'wechat'      ,//wechat小程序端   app App端
								code
							}).then(res=>{
								console.log(res.data);
								
								uni.requestPayment({
									provider:"wxpay",
									timeStamp:res.data.timeStamp,
									nonceStr:res.data.nonceStr,
									package:res.data.package,
									signType:res.data.signType,
									paySign:res.data.paySign,
									success:res=>{
										console.log(res,'cascsa');
										this.$u.toast('支付成功')
										setTimeout(_=>{
											uni.navigateBack()
										},700)
									},
									fail:err=>{
										console.log(err,'err');
										this.$u.toast('取消支付')
									}
								})
							})
						}
					})
				})
				
				
				// uni.navigateTo({
				// 	url:'../successBuy/successBuy'
				// })
			}
		}
	}
</script>

<style lang="scss">
	.w95{
		width: 95%;
		margin: 20rpx auto;
		padding: 20rpx;
		border-radius: 20rpx;
		background-color: #FFFFFF;
	}
	.comfirmOrder{
		.address{
			.top{
				display: flex;
				align-items: center;
				justify-content: space-between;
				.edit{
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
		.list{
			.shopinfo{
				display: flex;
				margin: 20rpx 0;
				font-weight: bold;
				.thumb{
					width: 150rpx;
					height: 150rpx;
					margin-right: 20rpx;
				}
				>view{
					flex: 1;
					.specs{
						
						margin-top: 20rpx;
						display: flex;
						justify-content: space-between;
						text:nth-child(1){
							font-weight: 100;
							font-size: 24rpx;
						}
					}
				}
			}
			.msg{
				display: flex;
				align-items: center;
				input{
					flex: 1;
					margin-left: 30rpx;
					border-radius: 10rpx;
					background-color: #F8F8F7;
					padding: 10rpx 30rpx;
				}
			}
		}
	
		.isXy{
			width: 93%;
			margin: auto;
			image{
				margin-right:10rpx ;
				width: 25rpx;
				height: 25rpx;
			}
			.xy{
				color:#326FB3;
			}
			
		}
	
		.fooder{
			background-color: #FFFFFF;
			position: fixed;
			bottom: 0;
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			padding: 20rpx;
			>text{
				color: rgba(0,0,0,.5);
			}
			.total{
				display: flex;
				margin: 0 50rpx 0 30rpx ;
				text{
					font-weight: bold;
					&:first-child{
						color: #333333;
					}
					&:last-child{
						color: $h-price;
					}
				}
			}
			.submit{
				width: auto;
				margin: 0;
				padding: 20rpx 50rpx;
			}
		}
	}
</style>
